package pe.edu.upc.dew.grupo04.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.support.WebApplicationContextUtils;
import pe.edu.upc.dew.grupo04.dao.SalaDao;
import pe.edu.upc.dew.grupo04.model.Sala;
import pe.edu.upc.dew.grupo04.service.SalaService;

public class SalaServlet extends HttpServlet implements SalaService {
	
	private static final long serialVersionUID = 1L;
	
	private SalaService salaService;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		// Integracion de Servlets con Spring
		this.salaService = WebApplicationContextUtils
				.getWebApplicationContext(config.getServletContext()).getBean(
						"salaService", SalaService.class);
	}
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SalaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	@Override
	public void setPeliculaDao(SalaDao salaDao) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void crearSala(Sala sala) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Sala getSalaPorCodigo(int codSala) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Sala> getSalas() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updateSala(Sala sala) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void eliminarSala(int codSala) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {		
			
			Sala sala = new Sala();
			sala.setCodSala(Integer.parseInt(request.getParameter("codSala")));
			sala.setNombre(request.getParameter("nombre"));
			sala.setCapacidad(Integer.parseInt(request.getParameter("capacidad")));
			salaService.crearSala(sala);
			request.getRequestDispatcher("sala.jsp").forward(request,response);
		}
		catch (Exception e) {
			String Cadena = " Existieron algunos Errores : Error" + e.getMessage();
			request.setAttribute("Cadena", Cadena);				
			request.getRequestDispatcher("pelicula.jsp").forward(request,
					response);
		}
		
	}
	

}
